﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using RTEditor;

public class Form_MaintainAreaUnit : MonoBehaviour
{
    private Transform addAnchorBtn; //增加锚点按钮
    private Transform delAnchorBtn; //减少锚点按钮
    private Transform saveBtn; //保存区域按钮
    private GameObject anchorPrefab; //锚点UI的预制体

    private RectTransform rt;
    private readonly int defaultAnchorNum = 3; //默认检修区域最小Anchr数量3
    private int curAnchrCount = 0;
    private AnchorGroup anchorGroup = new AnchorGroup(); //存取这个区域的锚点信息
    
    // Start is called before the first frame update
    void Start()
    {
        anchorPrefab = Resources.Load<GameObject>("AnchorItem");
        rt = GetComponent<RectTransform>();
        addAnchorBtn = transform.Find("Head/Add");
        delAnchorBtn = transform.Find("Head/Del");
        saveBtn = transform.Find("Head/Save");

        addAnchorBtn.GetComponent<Button>().onClick.AddListener(AddAnchorBtn_Click);
        delAnchorBtn.GetComponent<Button>().onClick.AddListener(DelAnchorBtn_Click);
        EventTriggerListener.Get(saveBtn).onClick += SaveAreaToDataManager;

        for (int i = 0; i < defaultAnchorNum; i++)
        {
            AddAnchorBtn_Click();
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void AddAnchorBtn_Click()
    {
        GameObject go = Instantiate(anchorPrefab, transform);
        curAnchrCount++;
        go.transform.Find("OrderBtn").GetComponentInChildren<Text>().text = string.Format("逆时针第{0}个", curAnchrCount);
        Transform orderBtn = go.transform.Find("OrderBtn");
        EventTriggerListener.Get(orderBtn).onClick += OrderBtn_Click;
        LayoutRebuilder.ForceRebuildLayoutImmediate(rt);
    }

    private void DelAnchorBtn_Click()
    {
        int count = transform.childCount;
        if (count > 4)
        {
            DestroyImmediate(transform.GetChild(count - 1).gameObject);
            LayoutRebuilder.ForceRebuildLayoutImmediate(rt.GetComponent<RectTransform>());
            curAnchrCount--;
        }
    }

    private void OrderBtn_Click(GameObject go)
    {
        Form_ConfigAreaPanel.Instance.curInputField = go.transform.parent.Find("InputField").GetComponent<InputField>();
    }

    private void InitDefaultMaintainArea()
    {

    }

    /// <summary>
    /// 保存到AnchorGroupManager
    /// </summary>
    private void SaveAreaToDataManager(GameObject go)
    {
        for (int i = 0; i < curAnchrCount; i++)
        {
            InputField IF = transform.GetChild(i + 1).GetComponentInChildren<InputField>();
            string anchorName = IF.text;
            if (anchorName != null)
            {
                if (AnchorManager.Instance.AllAnchorDict.TryGetValue(anchorName, out Anchor anchor)) //TODO这种存储方式有待改进
                {
                    anchorGroup.AnchorList.Add(anchor);
                }
                else
                {
                    Debug.LogError(string.Format("锚点{0}不存在", anchorName)); //如果保存之后，锚点被删除会报错
                    return;
                }
            }
            else
            {
                Debug.LogErrorFormat("请补全区域所有锚点的值！！");
                return;
            }
        }
        anchorGroup.AnchorCount = curAnchrCount;

        AnchorManager.Instance.DrawAnchorGroupArea(anchorGroup.AnchorList);
    }

    

}
